package top.kent90.controller;


import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.kent90.common.object.AjaxDone;
import top.kent90.common.object.Result;
import top.kent90.service.ZczyItemService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 后台项目管理
 */
@Controller
@RequestMapping("item")
public class ZczyItemController {
    @Reference
    ZczyItemService service;
    @RequestMapping("toOnlineItem")
    public String toOnlineItem(){return "zczyitem/onlineItem";}
    @RequestMapping("toItemDetail")
    public String toItemDetail(){
        return "zczyitem/itemDetail";
    }
    //到回收站  根据传入type 返回不同类型的回收站
    @RequestMapping(value = "toRecovery/{type}",method = RequestMethod.GET)
    public ModelAndView toRecovery(@PathVariable("type") String type, ModelAndView model){
        model.addObject("type",type);
        model.setViewName("zczyitem/recovery");
        return model;
    }

    /**
     * 综合权益类上线线项目列表
     * */
    @RequestMapping("onlineItem")
    @ResponseBody
    public Result onlienItem(HttpServletRequest request){
        //获取页码
        String pageNum = request.getParameter("page");
        //获取显示条数
        String pageSize = request.getParameter("limit");
        //获取项目名称
        String title = request.getParameter("title");

        Result rs = null;
        try {
                rs = service.queryShowItem(pageNum, pageSize, title,1,1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
    //项目详情
    @ResponseBody
    @RequestMapping("itemDetail")
    public Result itemDetail(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        Result rs = null;
        try {
            rs = service.queryItemById(Integer.parseInt(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }


    /**
     *     上下线操作
     * */
    @RequestMapping("updItemStatus")
    @ResponseBody
    public AjaxDone updItemStatus(HttpServletRequest request){
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        int rs = service.updItemStatus(id,status);
        if(rs>0){
            return new AjaxDone("200","请求成功");
        }else {
            return new AjaxDone("300","请求失败");
        }
    }
    /**
     *根据类型查询回收站
     *type：1 综合权益类  2：股权交易类
     */
    @RequestMapping("recovery")
    @ResponseBody
    public Result OnlineRecovery(HttpServletRequest request){
        //获取页码
        String pageNum = request.getParameter("page");
        //获取显示条数
        String pageSize = request.getParameter("limit");
        //获取项目名称
        String title = request.getParameter("title");
        String type = request.getParameter("type");

        Result rs = null;
        try {
            rs = service.queryShowItem(pageNum, pageSize, title,0,Integer.parseInt(type));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rs;
    }
}
